package gsw.demo.toolhttp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;

import gsw.demo.R;
import gsw.tool.http.call.Call;
import gsw.tool.http.call.Callback;
import gsw.tool.http.HttpClient;
import gsw.tool.http.request.Request;
import gsw.tool.http.request.RequestBody;
import gsw.tool.http.Response;
import gsw.tool.http.chain.Interceptor;
import gsw.tool.http.chain.InterceptorChain;

public class ActivityToolHttp extends AppCompatActivity {
    HttpClient client;
    //显示请求结果
    TextView showResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolhttp);
        init();
        initHttp();
    }

    private void initHttp() {
        client = new HttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public Response intercept(InterceptorChain chain) throws IOException {
                //dosomething
                Response proceed = chain.proceed();
                //dosomething
                return proceed;
            }
        }).build();
    }

    private void init() {
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.sendGet) {
                    get();
                } else if (v.getId() == R.id.sendPost) {
                    post();
                }
            }
        };
        findViewById(R.id.sendGet).setOnClickListener(clickListener);
        findViewById(R.id.sendPost).setOnClickListener(clickListener);

        showResult = findViewById(R.id.showResult);
        //让TextView可滑动
        showResult.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    /**
     * get请求
     */
    public void get() {
        Request request = new Request.Builder()
                .url("http://www.kuaidi100.com/query?type=yuantong&postid=11111111111")
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, Throwable throwable) {
                showReult(showResult, throwable.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) {
                showReult(showResult, response.getBody());
            }
        });
    }

    /**
     * post请求
     */
    public void post() {
        RequestBody body = new RequestBody()
                .add("city", "长沙")
                .add("key", "13cb58f5884f9749287abbead9c658f2");
        Request request = new Request.Builder().url("http://restapi.amap" +
                ".com/v3/weather/weatherInfo").post(body).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, Throwable throwable) {
                showReult(showResult, throwable.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) {
                showReult(showResult, response.getBody());
            }
        });
    }

    /**
     * 显示请求结果
     */
    private void showReult(final TextView tv, final String result) {
        tv.post(new Runnable() {
            @Override
            public void run() {
                tv.setText(result);
            }
        });
    }
}
